import { server } from '../config/server';
import { useToken } from '../utils/token';


export const circle = {
    /*
     * 获取圈子列表接口
     */
    getCircle(lastId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/getCircle`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { lastId },
                success: (res) => {
                    console.log('【 Response in request getCircle 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getCircle 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 获取圈子详细信息接口
     */
    getCircleInfo(circleId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/getCircleInfo`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { circleId },
                success: (res) => {
                    console.log('【 Response in request getCircleInfo 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getCircleInfo 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    comment(circleId, comment) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/comment`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { circleId, comment },
                success: (res) => {
                    console.log('【 Response in request comment 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request comment 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    getComment(circleId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/getComment`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { circleId },
                success: (res) => {
                    console.log('【 Response in request getComment 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getComment 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    likeCircle(circleId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/likeCircle`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { circleId },
                success: (res) => {
                    console.log('【 Response in request likeCircle 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request likeCircle 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    nolikeCircle(circleId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/nolikeCircle`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { circleId },
                success: (res) => {
                    console.log('【 Response in request nolikeCircle 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request nolikeCircle 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    addCircle(circleTitle, circleContent, selectedImage) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/addCircle`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { circleTitle, circleContent, selectedImage },
                success: (res) => {
                    console.log('【 Response in request addCircle 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request addCircle 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    deleteCircle(circleId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/circle/deleteCircle`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { circleId },
                success: (res) => {
                    console.log('【 Response in request deleteCircle 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request deleteCircle 】', err);
                    reject({ success: false, err });
                }
            });
        });
    }
};